package com.example.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.commonutils.R;
import com.example.eduservice.entity.EduCourse;
import com.example.eduservice.entity.EduTeacher;
import com.example.eduservice.entity.vo.CourseInfoVo;
import com.example.eduservice.entity.vo.CoursePublishVo;
import com.example.eduservice.entity.vo.TeacherQuery;
import com.example.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-06-16
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin//跨域
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    //课程列表 基本实现
    //TODO 完善条件查询带分页
    @GetMapping
    public R getCourseList() {
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list",list);
    }
//TODO 开始
    //条件查询带分页查询
    @PostMapping("pageCoursePublishCondition/{current}/{limit}")
    public R pageCoursePublishCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) CoursePublishVo coursePublishVo){
        //创建page对象
        Page<EduCourse> pageCoursePublish = new Page<>(current,limit);

        //构建条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //多条件组合查询
        //mybatis学过 动态sql
        //判断条件值是否为空，如果不为空拼接条件

        String id = coursePublishVo.getId();
        String title = coursePublishVo.getTitle();
        String cover = coursePublishVo.getCover();
        Integer lessonNum = coursePublishVo.getLessonNum();
        String subjectLevelOne = coursePublishVo.getSubjectLevelOne();
        String subjectLevelTwo = coursePublishVo.getSubjectLevelTwo();
        String teacherName = coursePublishVo.getTeacherName();
        String price = coursePublishVo.getPrice();

        if(!StringUtils.isEmpty(id)){
            //构建条件
            wrapper.like("id",id);
        }
        if(!StringUtils.isEmpty(title)){
            wrapper.eq("title",title);
        }
        if (!StringUtils.isEmpty(cover)) {
            wrapper.ge("cover",cover);
        }
        if (!StringUtils.isEmpty(lessonNum)){
            wrapper.le("lessonNum",lessonNum);
        }
        if(!StringUtils.isEmpty(subjectLevelOne)){
            //构建条件
            wrapper.like("subjectLevelOne",subjectLevelOne);
        }
        if(!StringUtils.isEmpty(subjectLevelTwo)){
            wrapper.eq("subjectLevelTwo",subjectLevelTwo);
        }
        if (!StringUtils.isEmpty(teacherName)) {
            wrapper.ge("teacherName",teacherName);
        }
        if (!StringUtils.isEmpty(price)){
            wrapper.le("price",price);
        }

        //排序
        wrapper.orderByDesc("id");
        //调用方法实现条件查询分页
        courseService.page(pageCoursePublish,wrapper);

        long total = pageCoursePublish.getTotal();//总记录数
        List<EduCourse> records = pageCoursePublish.getRecords();//数据list集合
        return R.ok().data("total",total).data("rows",records);
    }

    //TODO 结尾
    //添加课程基本信息的方法
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //返回添加之后课程id，为了后面添加大纲使用
        String id=courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    //根据课程id查询课程的基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo=courseService.getCourseInfo(courseId);
        return  R.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){//修改的时候用表单@RequestBody
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();

    }
    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo=courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    //课程最终发布
    //修改课程状态
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse=new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");//设置课程发布状态
        courseService.updateById(eduCourse);
        return R.ok();
    }

    //删除课程
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId);
        return R.ok();
    }

}

